package so.shanku.essential.activity;

import android.os.Bundle;
import android.view.View;
import android.widget.LinearLayout;
import aym.util.getservicesdata.GetServicesDataQueue;
import aym.util.getservicesdata.GetServicesDataUtil;
import aym.util.json.JsonMap;
import aym.util.json.JsonParseHelper;
import net.tsz.afinal.annotation.view.ViewInject;
import so.shanku.essential.R;
import so.shanku.essential.activity.base.BaseUIActivity;
import so.shanku.essential.adapter.UserOrderAdapter;
import so.shanku.essential.getdata.GetDataConfing;
import so.shanku.essential.getdata.GetDataQueue;
import so.shanku.essential.getdata.JsonKeys;
import so.shanku.essential.getdata.ShowGetDataError;
import so.shanku.essential.utils.Constant;
import so.shanku.essential.utils.Utils;
import striveen.util.used.view.view.XListView;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by Jaelyn on 2015/12/31 0031.
 * 申请退款记录
 */
public class ScanRefundRecordsActivity extends BaseUIActivity implements UserOrderAdapter.UserOrderItemCompountClickListener {
	@ViewInject(id = R.id.xlistview_order)
	private XListView xlistview_order;
	private UserOrderAdapter userOrderAdapter;
	/**
	 * 无相关数据
	 */
	@ViewInject(id = R.id.nodata_layout, click = "nodata_layout_click")
	private LinearLayout nodata_layout;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.refund_records_layout);
		setCenter_title(R.string.refund_records);
		xlistview_order.setXListViewListener(xListLoadMore);
		loadingToast.show();
		getData_UserBrand(true);
	}

	/**
	 * @param view
	 */
	public void nodata_layout_click(View view) {
//		loadingToast.show();
//		getData_UserBrand(true);
	}


	XListView.IXListViewListener xListLoadMore = new XListView.IXListViewListener() {
		@Override
		public void onRefresh() {
			getData_UserBrand(true);
		}

		@Override
		public void onLoadMore() {
			getData_UserBrand(false);
		}
	};


	/**
	 * 订单接口
	 */
	private int pageSize = 10;
	private int page = 0;
	private List<JsonMap<String, Object>> data_OrderList;

	private void getData_UserBrand(boolean chearAllData) {

		if (chearAllData) {
			data_OrderList = null;
			if (userOrderAdapter != null) {
				userOrderAdapter.notifyDataSetChanged();
			}
			page = 0;
			xlistview_order.setPullRefreshEnable(true);
			xlistview_order.setPullLoadEnable(true);
		}
		Map<String, Object> params = new HashMap<String, Object>();
		params.put(GetDataQueue.Params_key, GetDataConfing.Key_Str);
		params.put("userName", Utils.getUserId(this));
		params.put("OrderStatusId", 12);
		params.put("currentPage", page + 1);
		params.put("pageSize", pageSize);
		GetDataQueue queue = new GetDataQueue();
		queue.setCallBack(callBack);
		queue.setActionName(GetDataConfing.Action_GetOrderByOrderStatusId);
		queue.setWhat(GetDataConfing.What_getOrderByOrderStatusId);
		queue.setParamsNoJson(params);
		getDataUtil.getData(queue);
	}


	private void setAdatper_singleSortlist(final List<JsonMap<String, Object>> data) {

		if (data.size() == 0 || data.size() % pageSize != 0) {
			xlistview_order.setPullLoadEnable(false);
		}
		if (page == 0) {
			this.data_OrderList = data;
			userOrderAdapter = new UserOrderAdapter(this, data_OrderList);
			userOrderAdapter.setTag(2);
			xlistview_order.setAdapter(userOrderAdapter);
			userOrderAdapter.setUserOrderItemCompountClickListener(this);
		} else {
			this.data_OrderList.addAll(data);
			userOrderAdapter.notifyDataSetChanged();
		}
		page++;
	}

	/**
	 * 获取服务器数据的返回信息
	 */
	private GetServicesDataUtil.IGetServicesDataCallBack callBack = new GetServicesDataUtil.IGetServicesDataCallBack() {
		@Override
		public void onLoaded(GetServicesDataQueue entity) {
			if (entity.isOk()) {
				if (ShowGetDataError.isOkAndCodeIsNot1(mContext,
						entity.getInfo())) {
					if (entity.what == GetDataConfing.What_getOrderByOrderStatusId) {
						JsonMap jsonMap1 = JsonParseHelper.getJsonMap_JsonMap(entity.getInfo(), JsonKeys.Key_Info);
						List<JsonMap<String, Object>> data = jsonMap1.getList_JsonMap("OrderList");
						setAdatper_singleSortlist(data);
					} else if (entity.what == GetDataConfing.What_GetHistoryUserOrderByPageNum) {
						JsonMap jsonMap1 = JsonParseHelper.getJsonMap_JsonMap(entity.getInfo(), JsonKeys.Key_Info);
						List<JsonMap<String, Object>> data = jsonMap1.getList_JsonMap("OrderList");
						setAdatperDataByPageNum(data);
					}
				}
			} else {
				ShowGetDataError.showNetError(mContext);
			}
			loadingToast.dismiss();
			invalidateListData();
			xlistview_order.stopLoadMore();
			xlistview_order.stopRefresh();
		}
	};


	/**
	 * 设置数据 并华东到上个位置
	 *
	 * @param data
	 */
	private void setAdatperDataByPageNum(final List<JsonMap<String, Object>> data) {

		if (data.size() == 0 || data.size() % pageSize != 0) {
			xlistview_order.setPullLoadEnable(false);
		}
		this.data_OrderList = data;
		userOrderAdapter = new UserOrderAdapter(this, data_OrderList);
		userOrderAdapter.setTag(2);
		xlistview_order.setAdapter(userOrderAdapter);
		userOrderAdapter.setUserOrderItemCompountClickListener(this);
//        if(firstItemPosition<data_OrderList.size()) {
//            xlistview_order.setSelection(firstItemPosition);
//        }
	}

	/**
	 * 记录当前显示的第一个item的位置
	 */
	int firstItemPosition = 0;

	/**
	 * 获取历史以查询的订单
	 */
	private void getHistoryOders() {
		getData_UserBrand(true);
//        firstItemPosition = xlistview_order.getFirstVisiblePosition();
//        page = 0;
//        loadingToast.show();
//        data_OrderList = null;
//        if (userOrderAdapter != null) {
//            userOrderAdapter.notifyDataSetChanged();
//        }
//        xlistview_order.setPullRefreshEnable(true);
//        xlistview_order.setPullLoadEnable(true);
//        Map<String, Object> params = new HashMap<String, Object>();
//        params.put(GetDataQueue.Params_key, "orderCreateTime");
//        params.put("userName", Utils.getUserAccount(this));
//        params.put("currentPage", page + 1);
//        params.put("pageSize", pageSize);
//        GetDataQueue queue = new GetDataQueue();
//        queue.setCallBack(callBack);
//        queue.setActionName(GetDataConfing.Action_GetHistoryUserOrderByPageNum);
//        queue.setWhat(GetDataConfing.What_GetHistoryUserOrderByPageNum);
//        queue.setParamsNoJson(params);
//        getDataUtil.getData(queue);
	}

	private void invalidateListData() {
		if (data_OrderList == null || data_OrderList.size() < 1) {
			xlistview_order.setVisibility(View.GONE);
			nodata_layout.setVisibility(View.VISIBLE);
		} else {
			xlistview_order.setVisibility(View.VISIBLE);
			nodata_layout.setVisibility(View.GONE);
		}
	}

	@Override
	protected void onRestart() {
		super.onRestart();
		if (isNeedRefreshData) {
			getHistoryOders();
			isNeedRefreshData = false;
		}
	}

	JsonMap jsonMap;
	private boolean isNeedRefreshData = false;

	@Override
	public void userOrderItemCompountClick(int position, Constant.UserOrderItemType userOrderItemType, int childPosition) {
		jsonMap = data_OrderList.get(position);
		String orderNum = jsonMap.getStringNoNull("OrderNum");
		String orderId = jsonMap.getStringNoNull("Id");
		isNeedRefreshData = true;
		if(userOrderItemType== Constant.UserOrderItemType.SEE_PRODUCT){
			isNeedRefreshData=false;
			ArrayList<JsonMap<String,Object>>dataList = (ArrayList<JsonMap<String, Object>>) jsonMap.getList_JsonMap("OrderdetailList");
			String productId=dataList.get(childPosition).getStringNoNull("ProductId");
			seeProductDetail(productId);
		}else if (userOrderItemType == Constant.UserOrderItemType.SEE_BRAND) {
			isNeedRefreshData = false;
			seeShopThemeDetail(jsonMap.getStringNoNull("VendorId"));
		}else if (userOrderItemType == Constant.UserOrderItemType.REFUND_DETAIL) {
			isNeedRefreshData = false;
			scanRefundDetail(orderNum);
		}else if (userOrderItemType == Constant.UserOrderItemType.SEE_ORDER_DETAIL) { //订单详情
			seeOrderDetail(orderNum);
		}  else if (userOrderItemType == Constant.UserOrderItemType.SEELogistics) {  //物流
			isNeedRefreshData = false;
			seeLogistics(orderNum);
		}else if (userOrderItemType == Constant.UserOrderItemType.CANCEL_ORDER) {  //cancel order
			getData_Cancel_Order(orderId, callBack);
		} else if (userOrderItemType == Constant.UserOrderItemType.ORDER_CONFIRM) { //confirm receive
			goodsReceiveConfirm(orderNum, callBack);
		} else if (userOrderItemType == Constant.UserOrderItemType.FUND_RETURN) {  //apply return fund
			getFundReturn(orderId, callBack);
		} else if (userOrderItemType == Constant.UserOrderItemType.GOODS_RETURN) {  //申请退货
			getGoodsReturn(orderNum, callBack);

		} else if (userOrderItemType == Constant.UserOrderItemType.GOTO_PAY) {   //支付

		} else if (userOrderItemType == Constant.UserOrderItemType.DO_GOODS_RETURN) { //进行退货
			doGoodsReturn(orderNum, jsonMap.getStringNoNull("VendorId"));
		}
	}

}